import pymongo
import math

class CSaveManager(object):
    def __init__(self):
        self.m_SaveList = []
    
    def OnSaveLoop(self):
        if not self.m_SaveList:
            return
        iSaveCnt = math.ceil(len(self.m_SaveList)/300)
        lstSave = self.m_SaveList[:iSaveCnt]
        self.m_SaveList = self.m_SaveList[iSaveCnt:]
        for oSaveObj in lstSave:
            oSaveObj.OnSave()

class CSaveObj(object):
    m_ColName = ""
    def __init__(self, iID):
        self.m_ID = iID
    
    def Save(self):
        data = {}
        data["ID"] = self.m_ID
        return data
    
    def Load(self, data):
        pass

    def OnSave(self):
        pass

class CMongosClient(object):
    def __init__(self):
        self.m_SaveList = []
        oClient = pymongo.MongoClient("mongodb://localhost:27017/")
        self.m_MyDB = oClient["testdb0"]
    
    def AddSaveData(self, sCol, data):
        self.m_SaveList.append((sCol, data))

    def SaveLoop(self):
        while True:
            if self.m_SaveList:
                sCol, data = self.m_SaveList.pop(0)
                self.m_MyDB[sCol].update_one(data)


def Init():
    global g_SaveManager
    g_SaveManager = CSaveManager()

def GetSaveManager():
    global g_SaveManager
    return g_SaveManager